<template xmlns="http://www.w3.org/1999/html">
  <div class="content">
    <div class="title">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>系统</b></el-breadcrumb-item>
        <el-breadcrumb-item>额度管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="main-content">
      <el-button @click="createFunction" size="primary" >新增额度</el-button>
      <el-table :data="manage" highlight-current-row style="margin-top: 20px">
        <el-table-column prop="staffName" label="业务经理" width="200">
        </el-table-column>
        <el-table-column prop="deptName" label="部门名称" width="200">
        </el-table-column>
        <el-table-column prop="minQuotaAmount" label="最小额度" width="200">
        </el-table-column>
        <el-table-column prop="maxQuotaAmount" label="最大额度" width="200">
        </el-table-column>
        <el-table-column prop="options" label="操作" min-width="120">
          <template scope="scope">
            <el-button @click="changeData(scope.$index,scope.row)" type="text" size="small" icon="edit"></el-button>
            <el-button @click="deleteData(scope.$index,scope.row)" type="text" size="small" icon="delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <el-dialog title="新增额度" :visible.sync="dialogFormVisible" size="tiny">
        <el-form :model="form" style="margin-left: 50px" label-position="left" label-width="80px">
          <el-form-item label="额度类型">
             <el-select v-model="form.selectedType" @change="selectType" placeholder="请选择类型" style="width: 200px">
               <el-option label="贷款申请额度" value="0"></el-option>
               <el-option label="通融额度" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务经理" v-show="isShowManage">
             <el-select v-model="form.selectedManage" @change="getStaffDept" placeholder="请选择经理" style="width: 200px">
              <el-option 
                  v-for="manage in manageList" 
                  :label="manage.label" 
                  :key="manage.value" 
                  :value="manage.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属部门" v-show="isShowManage">
            <el-input :disabled="true" v-model="form.deptName" auto-complete="off" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="额度">
            <el-input v-model="form.minLimit" v-show="isShowLimit" auto-complete="off" style="width: 95px"></el-input>
            <span v-show="isShowLimit">-</span>
            <el-input v-model="form.maxLimit" auto-complete="off" v-bind:style="{width: inputSize+'px'}"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
  import ElCol from 'element-ui/packages/col/src/col'
  import ElForm from '../../../node_modules/element-ui/packages/form/src/form.vue'
  import axios from 'axios'
  import $ from 'jquery'

  export default {
    components: {
      ElForm,
      ElCol
    },
    data () {
      return {
        deptId:'',
        inputSize:200,
        isShowLimit:false,
        isShowManage:true,
      	dialogFormVisible:false,
      	form:{quotaId:'',selectedType:'',deptName:'',deptId:'',selectedManage:'',minLimit:'',maxLimit:''},
      	manage:[{dataId:'',manageName:'',Dept:'',limit:''}],
        currentRow:{},
        manageList:[],
        selectedStaffId:'',
        selectedStaffName:''
      }
    },
    methods:{
    	createFunction(){
    		this.dialogFormVisible = true
        this.form={quotaId:'',selectedType:'',deptName:'',selectedManage:'',minLimit:'',maxLimit:'',deptId:''}
    	},

      selectType(temp){
        if(temp==0){
          this.isShowLimit = true
          this.inputSize = 95
          this.isShowManage = false
        }else{
          this.inputSize = 200
          this.isShowLimit = false
          this.isShowManage = true
        }
      },

      getStaffDept(temp){
        console.log(temp)
        let that = this
        $.each(this.manageList,function(index,manage){
          if(manage.value==temp){
            that.form.deptName = manage.deptName
            that.form.deptId = manage.deptId
            that.selectedStaffId = manage.value
            that.selectedStaffName = manage.label
          }
        })
      },

      getManagerList(){
        let that = this
        this.$http.get("/roles/selectStaffByRole/"+"ROLE_SALESMANAGER")
        .then((response)=>{
          $.each(response.data.data,function(index,value){
            that.manageList.push({label:value.staffName,value:value.staffId,deptId:value.deptId,deptName:value.deptName})
          })
        })
        .catch((error)=>{
          console.log(error)
        })
      },
//selectedType:'',deptName:'',limit:'',selectedManage:'',minLimit:'',maxLimit:'',deptId:''
    	changeData(index, row){
        this.currentRow = row
    		this.dialogFormVisible = true
        if(row.quotaType==0){
          this.form.selectedType='0'
          this.isShowLimit = true
          this.inputSize = 95
          this.isShowManage = false
        }
        if(row.quotaType==1){
          this.form.selectedType='1'
          this.inputSize = 200
          this.isShowLimit = false
          this.isShowManage = true
        }
        this.form.deptName = row.deptName
        this.form.selectedManage = row.staffName 
        this.form.minLimit = row.minQuotaAmount
        this.form.maxLimit = row.maxQuotaAmount
        this.form.deptId = row.deptId
        this.form.quotaId = row.quotaId
    	},

      submit(){
        if(this.form.maxLimit<this.form.minLimit){
          this.$message("数据不规范，小于最小值")
        }else{
          this._submit()
        }
      },

      _submit(){
        var content ={}
        content.quotaId = this.form.quotaId
        content.quotaType = this.form.selectedType 
        content.maxQuotaAmount = this.form.maxLimit
        
        if(this.form.selectedType==0){
          content.minQuotaAmount = this.form.minLimit
        }else if(this.form.selectedType==1){
          content.deptId = this.form.deptId
          content.deptName = this.form.deptName
          content.staffId = this.selectedStaffId
          content.staffName = this.selectedStaffName
          content.minQuotaAmount = 0
        }
        this.$http.post('/quota/addOrModifyAuditQuota',content)
        .then((response)=>{
          if(response.data.success){
            this.getListDetail()
            this.dialogFormVisible = false
          }else{
            this.$message(response.data.message)
          }
        })
        .catch((error)=>{
          console.log(error)
        })
      },

      deleteData(index,row){
        let that = this
        this.$http.get("/quota/delete/"+row.quotaId)
        .then((response)=>{
          if(response.data.success){
            this.$message("删除成功")
            that.getListDetail()
          }else{
            this.$message(response.data.message)
          }
        })
        .catch((error)=>{
          console.log(error)
        })
      },

      getListDetail(){
        this.$http.get('/quota/list',{
          params:{
            pageSize:10,
            pageNo:1
          }
        })
        .then((response)=>{
          console.log(response)
          this.manage = response.data.data.result
        })
        .catch((error)=>{
          console.log(error)
        })
      }
      
    },

    mounted() {
      this.getManagerList()
      this.getListDetail()
    }
  }
</script>

<style lang="scss" scoped>
.content {
  padding: 10px;
  background: #F7F7F7;
  .title {
    color: #99A9BF;
    font-size: 14px;
    line-height: 18px;
    margin: 8px 0px 10px 0px;
  }
  .main-content {
    background: #fff;
    position: fixed;
    top: 96px;
    bottom: 10px;
    left: 216px;
    right: 16px;
    overflow: auto;
    padding: 20px;
  }
}
</style>